package com.excel.utils;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

import static java.math.BigDecimal.ROUND_HALF_EVEN;

/**
 * @author yutao
 * @since 2020/4/29 5:45 下午
 */
public class Cent {

    public static final Integer SCALE = 2;

    /**
     * 金额值
     */
    private final Long value;

    public static Cent of(Integer value) {
        return new Cent(value.longValue());
    }

    public String toYuan() {
        return getPriceInYuan(value);
    }

    public Cent multiply(Integer multiplicand) {
        return Cent.of(value * multiplicand);
    }

    public Cent multiply(Cent multiplicand) {
        return Cent.of(value * multiplicand.value);
    }

    public Cent divide(Integer divisor) {
        BigDecimal currentCent = new BigDecimal(value);
        BigDecimal divide = currentCent.divide(new BigDecimal(divisor), SCALE, ROUND_HALF_EVEN);
        return Cent.of(divide.longValue());
    }

    public Cent divide(Cent divisor) {
        return divide(divisor.value.intValue());
    }

    public Cent minus(Cent cent) {
        return Cent.of(value - cent.value);
    }

    @Override
    public String toString() {
        return Objects.toString(value);
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof Long && value == ((Long) obj).longValue();
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }

    private Cent(Long value) {
        this.value = value;
    }

    public static Cent of(Long value) {
        return new Cent(value);
    }

    public Long getValue() {
        return this.value;
    }

    public static final BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100);

    public static final int TWO = 2;
    public static final int ONE = 1;

    public static String getPriceInYuan(Long cent) {
        if (cent == null) {
            return "";
        }
        BigDecimal realPrice = new BigDecimal(cent).divide(ONE_HUNDRED, TWO, RoundingMode.HALF_EVEN);
        return realPrice.toString();
    }
}
